<template>
	<div class="select-wrap">
		<div class="select-box">
			<el-table :data="orderList" border class="table-form">
				<el-table-column label="订单商品" min-width="220">
					<template slot-scope="scope">
						<div class="imglist-box">
							<div class="img-box" v-for="(item,index) in scope.row.orderGoods" :key="index">
								<el-image class="pic" fit="contain" :src="$api.loadFile(item.imgUrl)">
									<div slot="error" class="image-slot">
										<i class="el-icon-picture-outline"></i>
									</div>
								</el-image>
								<h4>{{item.goodsTitle}}</h4>
								<span class="date">
									数量：{{item.quantity}}, 规格：{{item.skuText}}
								</span>
							</div>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="合计金额" width="110">
					<template slot-scope="scope">
						{{sumOrderGoodsPrice(scope.row.orderGoods)}} 元
					</template>
				</el-table-column>
				<el-table-column fixed="right" width="80" align="center">
					<template slot="header" slot-scope="scope">
						<el-button type="primary" size="mini" icon="el-icon-plus" style="margin:auto;" @click="handleOrderDialogShow">添加</el-button>
					</template>
					<template slot-scope="scope">
						<el-link :underline="false" icon="el-icon-edit-outline" @click="editOrderRow(scope.$index, scope.row)"></el-link>
						<el-link :underline="false" icon="el-icon-delete" @click="removeOrderRow(scope.$index)"></el-link>
					</template>
				</el-table-column>
			</el-table>
		</div>
		
		<el-dialog title="添加订单" :destroy-on-close="true" :visible.sync="showOrderDialog" @close="handleOrderDialogClose" fullscreen append-to-body>
			<el-form ref="orderForm" :model="model" :rules="rules" :label-position="labelPosition" label-width="100px">
				<div class="content-box">
					<el-form-item prop="merchantId" label="所属商户">
						<dt-merchant-select v-model="model.merchantId"></dt-merchant-select>
					</el-form-item>
					<el-form-item prop="deliveryId" label="配送方式">
						<el-select v-model="model.deliveryId" placeholder="请选择...">
							<el-option v-for="item in deliveryList"
								:key="item.id"
								:label="item.title"
								:value="item.id">
							</el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="是否保价">
						<el-row class="select-list">
							<el-col :xs="24" :sm="12">
								<el-form-item prop="isInsure">
									<el-switch :active-value="1" :inactive-value="0" v-model="model.isInsure" @change="model.insurePrice=0"></el-switch>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" v-if="model.isInsure===1">
								<el-form-item prop="insurePrice">
									<el-input v-model="model.insurePrice" placeholder="必填，快递的保价金额">
										<template slot="prepend">
											保价金额
										</template>
										<template slot="append">
											元
										</template>
									</el-input>
								</el-form-item>
							</el-col>
						</el-row>
					</el-form-item>
					<el-form-item prop="postscript" label="用户附言">
						<el-input type="textarea" :rows="3" v-model="model.postscript" maxlength="512" show-word-limit placeholder="可空，会员对订单的要求备注"></el-input>
					</el-form-item>
					<el-form-item prop="acceptTime" label="送货时间">
						<el-radio-group v-model="model.acceptTime">
							<el-radio label="任意">任意</el-radio>
							<el-radio label="周一至周五">周一至周五</el-radio>
							<el-radio label="周末">周末</el-radio>
						</el-radio-group>
					</el-form-item>
					
					<el-form-item prop="orderGoods" label="普通商品" v-if="type==0">
						<el-table ref="goodsTable" :data="model.orderGoods" border class="table-form">
							<el-table-column label="商品名称" min-width="220">
								<template slot-scope="scope">
									<el-image class="pic" fit="contain" :src="$api.loadFile(scope.row.imgUrl)">
										<div slot="error" class="image-slot">
											<i class="el-icon-picture-outline"></i>
										</div>
									</el-image>
									<h4>{{scope.row.goodsTitle}}</h4>
									<span class="date">
										{{scope.row.goodsNo}}
									</span>
								</template>
							</el-table-column>
							<el-table-column label="规格" min-width="120">
								<template slot-scope="scope">
									<span class="nowrap">{{scope.row.skuText}}</span>
								</template>
							</el-table-column>
							<el-table-column prop="realPrice" label="售价" width="80"></el-table-column>
							<el-table-column label="数量" width="80">
								<template slot-scope="scope">
									<el-form-item :prop="'orderGoods.'+scope.$index+'.quantity'" :rules='rules.quantity'>
										<el-input size="small" v-model="scope.row.quantity"></el-input>
									</el-form-item>
								</template>
							</el-table-column>
							<el-table-column fixed="right" width="80" align="center">
								<template slot="header" slot-scope="scope">
									<el-button type="primary" size="mini" icon="el-icon-plus" style="margin:auto;" @click="handleDialogShow">添加</el-button>
								</template>
								<template slot-scope="scope">
									<el-link :underline="false" icon="el-icon-delete" @click="removeGoodsRow(scope.$index)"></el-link>
								</template>
							</el-table-column>
						</el-table>
					</el-form-item>
					<el-form-item prop="orderGoods" label="抢购商品" v-if="type==1">
						<el-table ref="speedTable" :data="model.orderGoods" border class="table-form">
							<el-table-column label="商品名称" min-width="220">
								<template slot-scope="scope">
									<el-image class="pic" fit="contain" :src="$api.loadFile(scope.row.imgUrl)">
										<div slot="error" class="image-slot">
											<i class="el-icon-picture-outline"></i>
										</div>
									</el-image>
									<h4>{{scope.row.goodsTitle}}</h4>
									<span class="date">
										{{scope.row.goodsNo}}
									</span>
								</template>
							</el-table-column>
							<el-table-column label="规格" min-width="120">
								<template slot-scope="scope">
									<span class="nowrap">{{scope.row.skuText}}</span>
								</template>
							</el-table-column>
							<el-table-column prop="realPrice" label="抢购价" width="80"></el-table-column>
							<el-table-column label="数量" width="80">
								<template slot-scope="scope">
									<el-form-item :prop="'orderGoods.'+scope.$index+'.quantity'" :rules='rules.quantity'>
										<el-input size="small" v-model="scope.row.quantity"></el-input>
									</el-form-item>
								</template>
							</el-table-column>
							<el-table-column fixed="right" width="80" align="center">
								<template slot="header" slot-scope="scope">
									<el-button type="primary" size="mini" icon="el-icon-plus" style="margin:auto;" @click="handleDialogShow">添加</el-button>
								</template>
								<template slot-scope="scope">
									<el-link :underline="false" icon="el-icon-delete" @click="removeGoodsRow(scope.$index)"></el-link>
								</template>
							</el-table-column>
						</el-table>
					</el-form-item>
					<el-form-item prop="orderGoods" label="预售商品" v-if="type==2">
						<el-table ref="speedTable" :data="model.orderGoods" border class="table-form">
							<el-table-column label="商品名称" min-width="220">
								<template slot-scope="scope">
									<el-image class="pic" fit="contain" :src="$api.loadFile(scope.row.imgUrl)">
										<div slot="error" class="image-slot">
											<i class="el-icon-picture-outline"></i>
										</div>
									</el-image>
									<h4>{{scope.row.goodsTitle}}</h4>
									<span class="date">
										{{scope.row.goodsNo}}
									</span>
								</template>
							</el-table-column>
							<el-table-column label="规格" min-width="120">
								<template slot-scope="scope">
									<span class="nowrap">{{scope.row.skuText}}</span>
								</template>
							</el-table-column>
							<el-table-column prop="realPrice" label="预售价" width="80"></el-table-column>
							<el-table-column label="数量" width="80">
								<template slot-scope="scope">
									<el-form-item :prop="'orderGoods.'+scope.$index+'.quantity'" :rules='rules.quantity'>
										<el-input size="small" v-model="scope.row.quantity"></el-input>
									</el-form-item>
								</template>
							</el-table-column>
							<el-table-column fixed="right" width="80" align="center">
								<template slot="header" slot-scope="scope">
									<el-button type="primary" size="mini" icon="el-icon-plus" style="margin:auto;" @click="handleDialogShow">添加</el-button>
								</template>
								<template slot-scope="scope">
									<el-link :underline="false" icon="el-icon-delete" @click="removeGoodsRow(scope.$index)"></el-link>
								</template>
							</el-table-column>
						</el-table>
					</el-form-item>
					<el-form-item prop="orderGoods" label="换购商品" v-if="type==3">
						<el-table ref="convertTable" :data="model.orderGoods" border class="table-form">
							<el-table-column label="商品名称" min-width="220">
								<template slot-scope="scope">
									<el-image class="pic" fit="contain" :src="$api.loadFile(scope.row.imgUrl)">
										<div slot="error" class="image-slot">
											<i class="el-icon-picture-outline"></i>
										</div>
									</el-image>
									<h4>{{scope.row.goodsTitle}}</h4>
									<span class="date">
										{{scope.row.goodsNo}}
									</span>
								</template>
							</el-table-column>
							<el-table-column label="规格" min-width="120">
								<template slot-scope="scope">
									<span class="nowrap">{{scope.row.skuText}}</span>
								</template>
							</el-table-column>
							<el-table-column prop="goodsPrice" label="原售价" width="80"></el-table-column>
							<el-table-column prop="quantity" label="数量" width="80"></el-table-column>
							<el-table-column fixed="right" width="80" align="center">
								<template slot="header" slot-scope="scope">
									<el-button type="primary" size="mini" icon="el-icon-plus" style="margin:auto;" @click="handleDialogShow">添加</el-button>
								</template>
								<template slot-scope="scope">
									<el-link :underline="false" icon="el-icon-delete" @click="removeGoodsRow(scope.$index)"></el-link>
								</template>
							</el-table-column>
						</el-table>
					</el-form-item>
				</div>
			</el-form>
			
			<el-dialog title="选择普通商品" :visible.sync="showGoodsDialog" @close="handleDialogClose" append-to-body fullscreen>
				<div class="search-box" style="margin-bottom:20px">
					<el-input placeholder="输入关健字" v-model="keyword" @keyup.enter.native="initGoodsData">
						<el-button slot="append" icon="el-icon-search" @click="initGoodsData"></el-button>
					</el-input>
				</div>
				<div class="content-box" style="padding:0">
					<el-card class="table-card">
						<el-table ref="goodsListTable" v-loading="loading" :data="goodsList" stripe @selection-change="handleGoodsSelectionChange" class="table-list">
							<el-table-column type="selection" width="45"></el-table-column>
							<el-table-column label="商品名称" min-width="220">
								<template slot-scope="scope">
									<el-image class="pic" fit="contain" :src="$api.loadFile(scope.row.shopGoods.imgUrl)">
										<div slot="error" class="image-slot">
											<i class="el-icon-picture-outline"></i>
										</div>
									</el-image>
									<h4>{{scope.row.shopGoods.title}}</h4>
									<span class="date">
										<i class="el-icon-time"></i>
										{{scope.row.shopGoods.addTime}}
									</span>
								</template>
							</el-table-column>
							<el-table-column label="规格" min-width="150">
								<template slot-scope="scope">
									<span class="nowrap">{{scope.row.skuText}}</span>
								</template>
							</el-table-column>
							<el-table-column prop="sellPrice" label="销售价" width="90"></el-table-column>
							<el-table-column prop="stockQuantity" label="库存" width="90"></el-table-column>
							<el-table-column label="状态" width="80" align="center">
								<template slot-scope="scope">
									<el-tag size="small" type="success" effect="dark" v-if="scope.row.shopGoods.status==0">上架</el-tag>
									<el-tag size="small" type="warning" effect="dark" v-else-if="scope.row.shopGoods.status==1">下架</el-tag>
									<el-tag size="small" type="warning" effect="dark" v-else-if="scope.row.shopGoods.status==2">待审</el-tag>
								</template>
							</el-table-column>
						</el-table>
					</el-card>
					<div class="pager-box">
						<el-pagination background
							@size-change="handleSizeChange"
							@current-change="handleCurrentChange"
							:current-page="pageIndex"
							:page-sizes="[10, 20, 50, 100]"
							:page-size="pageSize"
							layout="total, sizes, prev, pager, next, jumper"
							:total="totalCount">
						</el-pagination>
					</div>
				</div>
				<div slot="footer" class="dialog-footer">
					<el-button type="primary" icon="el-icon-edit" @click="handleGoodsConfirm">确 定</el-button>
					<el-button type="warning" @click="showGoodsDialog=false">取 消</el-button>
				</div>
			</el-dialog>
			<el-dialog title="选择限时抢购" :visible.sync="showSpeedDialog" @close="handleDialogClose" append-to-body fullscreen>
				<div class="search-box" style="margin-bottom:20px">
					<el-input placeholder="输入关健字" v-model="keyword" @keyup.enter.native="initSpeedData">
						<el-button slot="append" icon="el-icon-search" @click="initSpeedData"></el-button>
					</el-input>
				</div>
				<div class="content-box" style="padding:0">
					<el-card class="table-card">
						<el-table v-loading="loading" :data="speedList" stripe highlight-current-row @current-change="handleSpeedSelectChange" class="table-list">
							<el-table-column label="商品名称" min-width="220">
								<template slot-scope="scope">
									<el-image class="pic" fit="contain" :src="$api.loadFile(scope.row.goodsProduct.shopGoods.imgUrl)"
											  :preview-src-list="[$api.loadFile(scope.row.goodsProduct.shopGoods.imgUrl)]">
										<div slot="error" class="image-slot">
											<i class="el-icon-picture-outline"></i>
										</div>
									</el-image>
									<h4>{{scope.row.goodsProduct.shopGoods.title}}</h4>
									<span class="date">
										<i class="el-icon-time"></i>
										{{scope.row.addTime}}
									</span>
								</template>
							</el-table-column>
							<el-table-column prop="price" label="抢购价" width="90"></el-table-column>
							<el-table-column prop="goodsProduct.sellPrice" label="原售价" width="90"></el-table-column>
							<el-table-column prop="status" label="状态" width="80" align="center">
								<template slot-scope="scope">
									<i class="el-icon-check" v-if="scope.row.status==0"></i>
									<i class="el-icon-close" v-else></i>
								</template>
							</el-table-column>
							<el-table-column prop="endTime" label="结束时间" width="160"></el-table-column>
						</el-table>
					</el-card>
					<div class="pager-box">
						<el-pagination background
							@size-change="handleSizeChange"
							@current-change="handleCurrentChange"
							:current-page="pageIndex"
							:page-sizes="[10, 20, 50, 100]"
							:page-size="pageSize"
							layout="total, sizes, prev, pager, next, jumper"
							:total="totalCount">
						</el-pagination>
					</div>
				</div>
			</el-dialog>
			<el-dialog title="选择商品预售" :visible.sync="showPresellDialog" @close="handleDialogClose" append-to-body fullscreen>
				<div class="search-box" style="margin-bottom:20px">
					<el-input placeholder="输入关健字" v-model="keyword" @keyup.enter.native="initPresellData">
						<el-button slot="append" icon="el-icon-search" @click="initPresellData"></el-button>
					</el-input>
				</div>
				<div class="content-box" style="padding:0">
					<el-card class="table-card">
						<el-table v-loading="loading" :data="presellList" stripe highlight-current-row @current-change="handlePresellSelectChange" class="table-list">
							<el-table-column label="商品名称" min-width="220">
								<template slot-scope="scope">
									<el-image class="pic" fit="contain" :src="$api.loadFile(scope.row.goodsProduct.shopGoods.imgUrl)"
											  :preview-src-list="[$api.loadFile(scope.row.goodsProduct.shopGoods.imgUrl)]">
										<div slot="error" class="image-slot">
											<i class="el-icon-picture-outline"></i>
										</div>
									</el-image>
									<h4>{{scope.row.goodsProduct.shopGoods.title}}</h4>
									<span class="date">
										<i class="el-icon-time"></i>
										{{scope.row.addTime}}
									</span>
								</template>
							</el-table-column>
							<el-table-column prop="totalPrice" label="预售价" width="90"></el-table-column>
							<el-table-column prop="frontAmount" label="预付款" width="90"></el-table-column>
							<el-table-column prop="retainAmount" label="尾款" width="90"></el-table-column>
							<el-table-column prop="status" label="状态" width="80" align="center">
								<template slot-scope="scope">
									<i class="el-icon-check" v-if="scope.row.status==0"></i>
									<i class="el-icon-close" v-else></i>
								</template>
							</el-table-column>
							<el-table-column prop="actEndTime" label="结束时间" width="160"></el-table-column>
						</el-table>
					</el-card>
					<div class="pager-box">
						<el-pagination background
							@size-change="handleSizeChange"
							@current-change="handleCurrentChange"
							:current-page="pageIndex"
							:page-sizes="[10, 20, 50, 100]"
							:page-size="pageSize"
							layout="total, sizes, prev, pager, next, jumper"
							:total="totalCount">
						</el-pagination>
					</div>
				</div>
			</el-dialog>
			<el-dialog title="选择积分换购" :visible.sync="showConvertDialog" @close="handleDialogClose" append-to-body fullscreen>
				<div class="search-box" style="margin-bottom:20px">
					<el-input placeholder="输入关健字" v-model="keyword" @keyup.enter.native="initConvertData">
						<el-button slot="append" icon="el-icon-search" @click="initConvertData"></el-button>
					</el-input>
				</div>
				<div class="content-box" style="padding:0">
					<el-card class="table-card">
						<el-table v-loading="loading" :data="convertList" stripe highlight-current-row @current-change="handleConvertSelectChange" class="table-list">
							<el-table-column label="商品名称" min-width="220">
								<template slot-scope="scope">
									<el-image class="pic" fit="contain" :src="$api.loadFile(scope.row.goodsProduct.shopGoods.imgUrl)"
											  :preview-src-list="[$api.loadFile(scope.row.goodsProduct.shopGoods.imgUrl)]">
										<div slot="error" class="image-slot">
											<i class="el-icon-picture-outline"></i>
										</div>
									</el-image>
									<h4>{{scope.row.goodsProduct.shopGoods.title}}</h4>
									<span class="date">
										<i class="el-icon-time"></i>
										{{scope.row.addTime}}
									</span>
								</template>
							</el-table-column>
							<el-table-column prop="point" label="兑换积分" width="90"></el-table-column>
							<el-table-column prop="goodsProduct.sellPrice" label="原售价" width="90"></el-table-column>
							<el-table-column prop="status" label="状态" width="80" align="center">
								<template slot-scope="scope">
									<i class="el-icon-check" v-if="scope.row.status==0"></i>
									<i class="el-icon-close" v-else></i>
								</template>
							</el-table-column>
							<el-table-column prop="addTime" label="创建时间" width="160"></el-table-column>
						</el-table>
					</el-card>
					<div class="pager-box">
						<el-pagination background
							@size-change="handleSizeChange"
							@current-change="handleCurrentChange"
							:current-page="pageIndex"
							:page-sizes="[10, 20, 50, 100]"
							:page-size="pageSize"
							layout="total, sizes, prev, pager, next, jumper"
							:total="totalCount">
						</el-pagination>
					</div>
				</div>
			</el-dialog>
			
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" icon="el-icon-edit" @click="submitOrderForm">确 定</el-button>
				<el-button type="warning" @click="showOrderDialog=false">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import DtMerchantSelect from './DtMerchantSelect.vue'
	
	export default {
		components: {
			DtMerchantSelect
		},
		props: {
			value: {
				type: Array,
				default() {
					return [];
				}
			},
			type: {
				type: Number,
				default() {
					return null;
				}
			},
		},
		data() {
			return {
				activeName: 'info', //当前选项卡
				labelPosition: 'right', //表单对齐方式
				showOrderDialog: false,
				indexOrderDialog: -1,
				orderList: [],
				showGoodsDialog: false,
				goodsList: [],
				showSpeedDialog: false,
				speedList: [],
				showPresellDialog: false,
				presellList: [],
				showConvertDialog: false,
				convertList: [],
				loading: false,
				keyword: '',
				totalCount: 0,
				pageSize: 10,
				pageIndex: 1,
				deliveryList: [],
				multipleSelection: [],
				model: {
					merchantId: null,
					activeId: null,
					deliveryId: null,
					isInsure: 0,
					insurePrice: 0,
					postscript: null,
					acceptTime: null,
					orderGoods: [],
					useCouponId: null,
				},
				rules: {
					merchantId: [
						{ required: true, message: '请选择所属商户', trigger: 'change' }
					],
					deliveryId: [
						{ required: true, message: '请选择配送方式', trigger: 'change' }
					],
					insurePrice: [
						{ required: true, message: '请输入物流保价金额', trigger: 'blur' },
						{ pattern: /^(([1-9]|([1-9][0-9]{0,9}))((\.[0-9]{1,2})?))$/, message: '请输入正确金额，可保留两位小数', trigger: 'blur' }
					],
					acceptTime: [
						{ required: true, message: '请选择收货时间', trigger: 'change' }
					],
					quantity: [
						{ required: true, message: '请填写购买数量', trigger: 'blur' },
						{ pattern: /^[1-9]\d*$/, message: '只能为整数', trigger: 'blur' }
					],
					orderGoods: [
						{ type: 'array', required: true, message: '请添加订单的商品', trigger: 'change' },
					],
				}
			}
		},
		methods: {
			//初始化数据
			initData() {
				let _this = this;
				//加载配送列表
				_this.$api.request({
					url: '/admin/shop/delivery',
					success(res) {
						_this.deliveryList = res.data;
					}
				});
			},
			//加载订单弹窗
			handleOrderDialogShow() {
				let _this = this;
				if(!(_this.type >= 0)) {
					_this.$message({ type: 'warning', message: '请先选择订单的类型' });
					return false;
				}
				_this.showOrderDialog = true;
			},
			//关闭订单弹窗
			handleOrderDialogClose() {
				let _this = this;
				_this.indexOrderDialog = -1;
				_this.model.orderGoods = [];
				_this.$nextTick(() => {
					_this.$refs.orderForm.resetFields();
				})
			},
			//提交订单表单
			submitOrderForm() {
				let _this = this;
				 //表单验证
				 _this.$refs['orderForm'].validate((valid) => {
					if (!valid)  return false;
					if (_this.indexOrderDialog >= 0) {
						//修改对象
						Object.keys(_this.model).forEach(key => {
							_this.orderList[_this.indexOrderDialog][key] = _this.model[key]
						});
					} else {
						//复制对象
						_this.orderList.push(JSON.parse(JSON.stringify(_this.model)));
					}
					_this.$emit('input', _this.orderList);
					_this.showOrderDialog = false;
				});
			},
			//移除订单一行
			removeOrderRow(index) {
				this.orderList.splice(index, 1);
				this.$emit('input', this.orderList);
			},
			//编辑订单
			editOrderRow(index, item) {
				let _this = this; //当前页面
				_this.showOrderDialog = true;
				_this.$nextTick(() => {
					Object.keys(_this.model).forEach(key => { _this.model[key] = item[key] });
					_this.indexOrderDialog = index;
				});
			},
			//计算订单总额
			sumOrderGoodsPrice(val) {
				if(!val) return 0;
				let totalPrice = 0;
				val.map((item) => {
					totalPrice += (item.realPrice * item.quantity);
				})
				return totalPrice;
			},
			//加载弹窗
			handleDialogShow() {
				let _this = this;
				if(!_this.model.merchantId) {
					_this.$message({ type: 'warning', message: '请先选择所属的商户' });
					return false;
				}
				if(_this.type==0) {
					_this.initGoodsData();
					_this.showGoodsDialog = true;
				}
				if(_this.type==1) {
					_this.initSpeedData();
					_this.showSpeedDialog = true;
				}
				if(_this.type==2){
					_this.initPresellData();
					_this.showPresellDialog = true;
				}
				if(_this.type==3) {
					_this.initConvertData();
					_this.showConvertDialog = true;
				}
			},
			//关闭弹窗回调
			handleDialogClose() {
				this.keyword = '';
			},
			//删除商品
			removeGoodsRow(index) {
				this.model.orderGoods.splice(index, 1);
			},
			//每页显示数量
			handleSizeChange(val) {
				if (this.pageSize != val) {
					this.pageSize = val;
					if(this.type==0) {
						this.initGoodsData();
					}
					if(this.type==1) {
						this.initSpeedData();
					}
					if(this.type==2) {
						this.initPresellData();
					}
					if(this.type==3) {
						this.initConvertData();
					}
				}
			},
			//跳转到第几页
			handleCurrentChange(val) {
				if (this.pageIndex != val) {
					this.pageIndex = val;
					if(this.type==0) {
						this.initGoodsData();
					}
					if(this.type==1) {
						this.initSpeedData();
					}
					if(this.type==2) {
						this.initPresellData();
					}
					if(this.type==3) {
						this.initConvertData();
					}
				}
			},
			//加载商品列表
			initGoodsData() {
				let _this = this;
				let sendUrl = "/admin/shop/goods/product";
				sendUrl += `?status=0&merchantId=${_this.model.merchantId}`;
				sendUrl += "&pageSize=" + _this.pageSize;
				sendUrl += "&pageIndex=" + _this.pageIndex;
				if (_this.keyword.length > 0) {
					sendUrl += "&keyword=" + encodeURI(_this.keyword);
				}
				_this.$api.request({
					url: sendUrl,
					beforeSend() {
						_this.loading = true
					},
					success(res) {
						_this.goodsList = res.data;
						let pageInfo = JSON.parse(res.headers["x-pagination"]);
						_this.totalCount = pageInfo.totalCount;
						_this.pageSize = pageInfo.pageSize;
						_this.pageIndex = pageInfo.pageIndex;
						//选中已选项
						_this.$nextTick(() => {
							if (_this.model.orderGoods.length > 0) {
								_this.goodsList.map(item => {
									let hasKey = false;
									_this.model.orderGoods.map(row => {
										if (row.productId === item.id) {
											hasKey = true;
										}
									})
									if (hasKey) {
										_this.$refs.goodsListTable.toggleRowSelection(item, true);
									}
								});
							}
						});
					},
					error(err) {
						_this.goodsList = [];
					},
					complete() {
						_this.loading = false
					}
				});
			},
			//选中商品回调
			handleGoodsSelectionChange(val) {
				this.multipleSelection = val;
			},
			//确认商品回调
			handleGoodsConfirm() {
				let _this = this; //当前页面
				//拿到选中的数据
				let list = _this.multipleSelection;
				//检查是否有选中
				if (!list.length) {
					_this.$message({ type: 'warning', message: '请选择要添加的商品！' });
					return false;
				}
				list.map((item) => {
					let hasKey = false;
					_this.model.orderGoods.map(row => {
						if (row.productId === item.id) {
							hasKey = true;
						}
					});
					if (!hasKey) {
						_this.model.orderGoods.push({
							id: 0,
							orderId: 0,
							productId: item.id,
							goodsNo: item.goodsNo,
							goodsTitle: item.shopGoods.title,
							skuText: item.skuText,
							imgUrl: item.imgUrl ? item.imgUrl : item.shopGoods.imgUrl,
							goodsPrice: item.sellPrice,
							realPrice: item.sellPrice,
							quantity: 1,
							weight: item.weight,
						});
					}
				});
				_this.model.activeId = 0;
				_this.showGoodsDialog = false;
			},
			//加载抢购列表
			initSpeedData() {
				let _this = this;
				let sendUrl = "/admin/shop/speed";
				sendUrl += `?status=0&merchantId=${_this.model.merchantId}`;
				sendUrl += "&pageSize=" + _this.pageSize;
				sendUrl += "&pageIndex=" + _this.pageIndex;
				if (_this.keyword.length > 0) {
					sendUrl += "&keyword=" + encodeURI(_this.keyword);
				}
				_this.$api.request({
					url: sendUrl,
					beforeSend() {
						_this.loading = true
					},
					success(res) {
						_this.speedList = res.data;
						let pageInfo = JSON.parse(res.headers["x-pagination"]);
						_this.totalCount = pageInfo.totalCount;
						_this.pageSize = pageInfo.pageSize;
						_this.pageIndex = pageInfo.pageIndex;
					},
					error(err) {
						_this.speedList = []
					},
					complete() {
						_this.loading = false
					}
				});
			},
			//选中抢购活动回调
			handleSpeedSelectChange(val) {
				let _this = this;
				if (val) {
					_this.model.activeId = val.id;//抢购活动ID
					_this.model.orderGoods = [];//清空订单商品
					//添加商品列表
					_this.model.orderGoods.push({
						id: 0,
						orderId: 0,
						productId: val.goodsProduct.id,
						goodsNo: val.goodsProduct.goodsNo,
						goodsTitle: val.goodsProduct.shopGoods.title,
						skuText: val.goodsProduct.skuText,
						imgUrl: val.goodsProduct.imgUrl ? val.goodsProduct.imgUrl : val.goodsProduct.shopGoods.imgUrl,
						goodsPrice: val.goodsProduct.sellPrice,
						realPrice: val.price,
						quantity: 1,
						weight: val.goodsProduct.weight,
					});
					_this.model.activeId = val.id;
					_this.showSpeedDialog = false;
				}
			},
			//加载预售列表
			initPresellData() {
				let _this = this;
				let sendUrl = "/admin/shop/presell";
				sendUrl += `?status=0&merchantId=${_this.model.merchantId}`;
				sendUrl += "&pageSize=" + _this.pageSize;
				sendUrl += "&pageIndex=" + _this.pageIndex;
				if (_this.keyword.length > 0) {
					sendUrl += "&keyword=" + encodeURI(_this.keyword);
				}
				_this.$api.request({
					url: sendUrl,
					beforeSend() {
						_this.loading = true
					},
					success(res) {
						_this.presellList = res.data;
						let pageInfo = JSON.parse(res.headers["x-pagination"]);
						_this.totalCount = pageInfo.totalCount;
						_this.pageSize = pageInfo.pageSize;
						_this.pageIndex = pageInfo.pageIndex;
					},
					error(err) {
						_this.presellList = []
					},
					complete() {
						_this.loading = false
					}
				});
			},
			//选中预售活动回调
			handlePresellSelectChange(val) {
				let _this = this;
				if (val) {
					_this.model.activeId = val.id;//抢购活动ID
					_this.model.orderGoods = [];//清空订单商品
					//添加商品列表
					_this.model.orderGoods.push({
						id: 0,
						orderId: 0,
						productId: val.goodsProduct.id,
						goodsNo: val.goodsProduct.goodsNo,
						goodsTitle: val.goodsProduct.shopGoods.title,
						skuText: val.goodsProduct.skuText,
						imgUrl: val.goodsProduct.imgUrl ? val.goodsProduct.imgUrl : val.goodsProduct.shopGoods.imgUrl,
						goodsPrice: val.goodsProduct.sellPrice,
						realPrice: val.totalPrice,
						quantity: 1,
						weight: val.goodsProduct.weight,
					});
					_this.model.activeId = val.id;
					_this.showPresellDialog = false;
				}
			},
			//加载换购列表
			initConvertData() {
				let _this = this;
				let sendUrl = "/admin/shop/convert";
				sendUrl += `?status=0&merchantId=${_this.model.merchantId}`;
				sendUrl += "&pageSize=" + _this.pageSize;
				sendUrl += "&pageIndex=" + _this.pageIndex;
				if (_this.keyword.length > 0) {
					sendUrl += "&keyword=" + encodeURI(_this.keyword);
				}
				_this.$api.request({
					url: sendUrl,
					beforeSend() {
						_this.loading = true
					},
					success(res) {
						_this.convertList = res.data;
						let pageInfo = JSON.parse(res.headers["x-pagination"]);
						_this.totalCount = pageInfo.totalCount;
						_this.pageSize = pageInfo.pageSize;
						_this.pageIndex = pageInfo.pageIndex;
					},
					error(err) {
						_this.convertList = []
					},
					complete() {
						_this.loading = false
					}
				});
			},
			//选中换购活动回调
			handleConvertSelectChange(val) {
				let _this = this;
				if (val) {
					_this.model.activeId = val.id;//抢购活动ID
					_this.model.orderGoods = [];//清空订单商品
					//添加商品列表
					_this.model.orderGoods.push({
						id: 0,
						orderId: 0,
						productId: val.goodsProduct.id,
						goodsNo: val.goodsProduct.goodsNo,
						goodsTitle: val.goodsProduct.shopGoods.title,
						skuText: val.goodsProduct.skuText,
						imgUrl: val.goodsProduct.imgUrl ? val.goodsProduct.imgUrl : val.goodsProduct.shopGoods.imgUrl,
						goodsPrice: val.goodsProduct.sellPrice,
						realPrice: 0,
						quantity: 1,
						weight: val.goodsProduct.weight,
					});
					_this.model.activeId = val.id;
					_this.showConvertDialog = false;
				}
			},
		},
		created() {
			this.initData();
		},
		watch: {
			type: {
				immediate: true,
				handler(newVal) {
					this.orderList = [];
				}
			},
		}
	}
</script>

<style lang="scss">
	.select-wrap {
		.el-form-item {
			margin-bottom: 22px;
			.el-form-item {
				margin-bottom: 0;
			}
		}
	}
	.imglist-box {
		display: flex;
		flex-flow: row wrap;
		.img-box {
			margin: 5px 0;
		}
	}
</style>